package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallRegionMapper;
import org.linlinjava.litemall.db.domain.LitemallRegion;
import org.linlinjava.litemall.db.domain.LitemallRegionExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Service
public class LitemallRegionService {

    @Resource
    private LitemallRegionMapper regionMapper;

    public List<LitemallRegion> getAll(){
        LitemallRegionExample example = new LitemallRegionExample();
        byte b = 4;
        example.or().andTypeNotEqualTo(b);
        return regionMapper.selectByExample(example);
    }

    public List<LitemallRegion> queryByPid(Integer parentId) {
        LitemallRegionExample example = new LitemallRegionExample();
        example.or().andPidEqualTo(parentId);
        return regionMapper.selectByExample(example);
    }

    public List<LitemallRegion> queryByParentCode(Integer pCode) {
        LitemallRegion region = findByCode(pCode);
        return queryByPid(region.getId());
    }

    public LitemallRegion findById(Integer id) {
        return regionMapper.selectByPrimaryKey(id);
    }

    public String findNameByCode(Integer code) {
        return findByCode(code).getName();
    }

    public List<Integer> findCodeByName(String provinceName, String cityName, String countryName) {
        LitemallRegionExample example = new LitemallRegionExample();
        example.or().andNameEqualTo(provinceName);
        LitemallRegion province = regionMapper.selectOneByExample(example);
        List<LitemallRegion> cityList = queryByPid(province.getId());
        for (LitemallRegion city : cityList) {
            if (city.getName().equals(cityName)) {
                List<LitemallRegion> countryList = queryByParentCode(city.getId());
                for (LitemallRegion country : countryList) {
                    if (country.getName().equals(countryName)) {
                        List<Integer> temp = new LinkedList<>();
                        temp.add(province.getCode());
                        temp.add(city.getCode());
                        temp.add(country.getCode());
                        return temp;
                    }
                }
                List<Integer> temp = new LinkedList<>();
                temp.add(province.getCode());
                temp.add(city.getCode());
                temp.add(null);
                return temp;
            }
        }
        List<Integer> temp = new LinkedList<>();
        temp.add(province.getCode());
        temp.add(null);
        temp.add(null);
        return temp;
    }

    public LitemallRegion findByCode(Integer code) {
        LitemallRegionExample example = new LitemallRegionExample();
        example.or().andCodeEqualTo(code);
        return regionMapper.selectOneByExample(example);
    }

    public List<LitemallRegion> querySelective(String name, Integer code, Integer page, Integer size, String sort, String order) {
        LitemallRegionExample example = new LitemallRegionExample();
        LitemallRegionExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtils.isEmpty(code)) {
            criteria.andCodeEqualTo(code);
        }

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, size);
        return regionMapper.selectByExample(example);
    }

    public void add(LitemallRegion region) {
        regionMapper.insertSelective(region);
    }

}
